<?php

/**
 * sfActionsCrud.
 *
 * @package    flexiwik
 * @subpackage actions
 * @author     Jonathan Olger Nieto Lajo <jonathan_nieto@hotmail.com>
 */
abstract class sfActionsCrud extends sfActionsExt
{
  protected
    $uClass    = '', // underscore class name
    $dbClass   = '',
    $stcClass  = '',
    $formClass = '',
    $ns        = '';
    
  public function initialize($context, $moduleName, $actionName)
  {
  	parent::initialize($context, $moduleName, $actionName);
  	
    $class_name      = get_class($this);
    $class_name      = substr($class_name, 0, strpos($class_name, 'Actions'));
    $this->uClass    = sfInflector::underscore($class_name);
    $this->dbClass   = $class_name;
    $this->stcClass  = $this->dbClass.'Table';
    $this->formClass = $this->dbClass.'Form';
    $this->ns        = constant(sprintf('self::%s_NAMESPACE', strtoupper($this->uClass)));
    
    $request = sfContext::getInstance()->getRequest();
    $request->setParameter('uClass'  , $this->uClass);
    $request->setParameter('dbClass' , $this->dbClass);
    $request->setParameter('stcClass', $this->stcClass);
    $request->setParameter('ns'      , $this->ns);
  }
  
  public function executeList(sfWebRequest $request)
  {
    $q = new kcDoctrineQuery();


    $q->filterAndArrange($request->getParameterHolder()->getAll(), $this->getExtraFilterAndArrangeFields());
    $this->executeIfExists('complementList', $request, $q);
  	$this->setPager($request, $q);
  }
  public function executeEdit(sfWebRequest $request)
  {
  	$slug       = $request->getParameter('slug', '');
  	$this->obj  = Doctrine::getTable($this->dbClass)->findOneBySlug($slug);
  	$this->executeIfExists('complementObj', $request);
  	
  	$this->form = new $this->formClass($this->obj);
  	$this->executeIfExists('complementEdit', $request);
  	if ($request->isMethod('post'))
  	{
  	  $params = $request->getParameter($this->form->getName());
  	    
  	  $this->form->bind($params, $request->getFiles($this->form->getName()));
  	  
  	  if ($this->form->isValid())
  	  {
  	    $this->executeIfExists('beforeSave', $request, null);
  	  	$obj = $this->form->save();
  	  	$this->executeIfExists('complementSave', $request, null);
  	  	$this->redirect($this->getEntrance());
  	  }
  	}
  }
  public function executeDelete(sfWebRequest $request)
  {
  	$slugs = $request->getParameter('slug');
  	$slugs = explode(',', $slugs);
  	$this->forward404Unless($slugs);
  	
  	try
  	{
  	  Doctrine::getTable($this->dbClass)->deleteBySlugs($slugs);
  	}
  	catch (Exception $e)
  	{
  	  //$this->getUser()->setFlash('name_error', $obj->getName());
  	  $this->redirect('@error_delete_error');
  	  //Deb::print_r($e->getMessage());
  	}
  	
  	$this->executeIfExists('complementDelete', $request);
  	
  	$this->redirect($this->getEntrance());
  }
  
  protected function setPager(sfWebRequest $request, kcDoctrineQuery $q)
  {
  	$pager = new sfDoctrinePager($this->dbClass, $request->getParameter('max'));
  	$pager->setPage($request->getParameter('page'));
  	$pager->setQuery($q);
  	$pager->init();
  	$this->pager = $pager;
  }
  protected function getEntrance($module = null)
  {
    $module = $module ? $module : $this->dbClass;
    
    return Toolkit::getEntranceRoute($module);
  }
  
  protected function getExtraFilterAndArrangeFields()
  {
    return array();
  }
  
  protected function changeModule($module)
  {
    $this->dbClass  = $module;
    $this->stcClass = $this->dbClass.'Table';
    $request = sfContext::getInstance()->getRequest();
    $request->setParameter('module'  , $module);
    $request->setParameter('dbClass' , $this->dbClass);
    $request->setParameter('stcClass', $this->stcClass);
  }
}
